/*
 * @Author: ReinerLau lk850593913@gmail.com
 * @Date: 2023-01-09 17:01:42
 * @LastEditors: ReinerLau lk850593913@gmail.com
 * @LastEditTime: 2023-01-16 11:29:50
 * @FilePath: \v2.0.0\cypress\e2e\patroling\patroling.cy.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
describe("围界车", () => {
  beforeEach(() => {
    const username = "admin";
    const password = "123456";
    cy.login(username, password);

    cy.visit("/patroling/release");

    cy.getByData("patroling-car-switch").find("svg").eq(0).click();

    cy.getByData("patroling-bottom").find("div").eq(1).click();

    cy.wait(3 * 1000);
  });

  it("新建模板", () => {
    const templateName = "test";

    cy.intercept("POST", "/api/robot-mission-template/v1/").as(
      "createTemplate"
    );

    createTemplate(templateName);

    cy.wait("@createTemplate")
      .its("request.body.name")
      .should("eq", templateName);
  });

  it("选择模板", () => {
    cy.intercept("POST", "/api/robot-cruise/patrolingCruise/v2/**").as(
      "createMission"
    );

    createTemplate("test");

    selectTemplate();

    cy.contains("自主规划").click();

    cy.contains("下发任务").click();

    cy.wait("@createMission").its("request.body.length").should("eq", 2);
  });

  it("修改模板", () => {
    cy.intercept("PUT", "/api/robot-mission-template/v1/", (req) => {
      const { body } = req;
      expect(JSON.parse(body.mission).length).to.equal(2);
    });

    updateTemplate("test");

    cy.get(".el-message-box").find("button").contains("否").click();
  });

  it("额外新增模板", () => {
    const templateName = "test1";

    updateTemplate(templateName);

    cy.get(".el-message-box").find("button").contains("是").click();

    cy.contains("任务模板名称").siblings().find("input").type(templateName);

    cy.contains("任务描述").siblings().find("input").type(templateName);

    cy.get("[aria-label='新建模板']").find("button").contains("确定").click();

    cy.contains("发送成功").should("exist");
  });

  it("删除模板", () => {
    cy.intercept("DELETE", "/api/robot-mission-template/v1/*").as(
      "deleteTemplate"
    );

    createTemplate("test");

    searchTemplate();

    cy.getByData("template-item").then(($item) => {
      const listLength = $item.length;

      cy.getByData("template-delete").eq(-1).click();

      cy.get(".el-message-box").find("button").contains("确定").click();

      cy.wait("@deleteTemplate");

      cy.getByData("template-item").should("have.length", listLength - 1);
    });
  });
});

function searchTemplate() {
  cy.contains("模板").click();

  cy.contains("搜索模板").click();

  cy.getByData("template-list").scrollTo("bottom");
}

function createTemplate(templateName) {
  cy.contains("模板").click();

  cy.contains("新建模板").click();

  cy.contains("任务模板名称").siblings().find("input").type(templateName);

  cy.contains("任务描述").siblings().find("input").type(templateName);

  cy.get("[aria-label='新建模板']").find("button").contains("确定").click();

  cy.getByData("patroling-map").as("map");

  cy.get("@map").click(500, 500);

  cy.get("@map").dblclick(700, 500);

  cy.contains("模板").click();

  cy.contains("保存模板").click();
}

function selectTemplate() {
  searchTemplate();

  cy.getByData("template-item").eq(-1).click();

  cy.contains("确定").click();
}

function updateTemplate(templateName) {
  createTemplate(templateName);

  selectTemplate();

  cy.getByData("patroling-map").as("map");
  cy.get("@map").trigger("mousedown", 700, 500, {
    eventConstructor: "MouseEvent",
  });
  cy.get("@map").trigger("mousemove", 700, 600);
  cy.get("@map").trigger("mouseup", 700, 600);

  cy.contains("模板").click();

  cy.contains("保存模板").click();
}
